package com.codedancer.campus.manager.runner;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONUtil;
import com.codedancer.campus.common.constant.DataCacheKey;
import com.codedancer.campus.common.utils.RedisUtil;
import com.codedancer.campus.common.utils.SpringContextUtils;
import com.codedancer.campus.mapper.AdminMapper;
import com.codedancer.campus.mapper.AdminRoleMapper;
import com.codedancer.campus.mapper.MenuMapper;
import com.codedancer.campus.mapper.RoleMapper;
import com.codedancer.campus.mapper.RoleMenuMapper;
import com.codedancer.campus.po.Admin;
import com.codedancer.campus.po.AdminRole;
import com.codedancer.campus.po.Menu;
import com.codedancer.campus.po.Role;
import com.codedancer.campus.po.RoleMenu;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 基础数据自动导入 admin、role、menu、adminRole、roleMenu
 */
@Component
@Slf4j
@Order(2)
public class BasicDataAutoImportRunner implements ApplicationRunner {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private AdminRoleMapper adminRoleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private RoleMenuMapper roleMenuMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Resource(name = "dataCacheRedisTemplate")
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 自动导出基础数据
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (SpringContextUtils.getActiveProfile().equals("local")) {
            // 只有启动本地环境才导入
            // 从redis中获取是否已导入
            log.info("项目启动，开始自动执行导入基础数据");
            if (redisUtil.isExist(DataCacheKey.BASIC_DATA_IS_IMPORT)) {
                String result = redisUtil.getStringValue(DataCacheKey.BASIC_DATA_IS_IMPORT);
                if (result.equals("true")) {
                    log.info("基础数据已被导入");
                    return;
                }
            }
            log.info("读取admin.json配置文件");
            // 读取admin.json配置
            String adminJson = ResourceUtil.readUtf8Str("json/admin.json");
            List<Admin> admins = JSONUtil.toList(adminJson, Admin.class);
            log.info("开始导入管理员基础数据");
            // 保存到admin中
            for (Admin admin : admins) {
                if (adminMapper.existsByUuId(admin.getUuid())) {
                    continue;
                }
                adminMapper.insert(admin);
            }
            log.info("导入管理员基础数据完成");
            log.info("读取role.json配置文件");
            String roleJson = ResourceUtil.readUtf8Str("json/role.json");
            List<Role> roles = JSONUtil.toList(roleJson, Role.class);
            log.info("开始导入角色基础数据");
            for (Role role : roles) {
                if (roleMapper.existsByUuId(role.getUuid())) {
                    continue;
                }
                roleMapper.insert(role);
            }
            log.info("导入角色基础数据完成");
            // 读取role.json配置
            log.info("读取admin_role.json配置文件");
            // 读取admin_role.json配置
            String adminRoleJson = ResourceUtil.readUtf8Str("json/admin_role.json");
            List<AdminRole> adminRoles = JSONUtil.toList(adminRoleJson, AdminRole.class);
            log.info("开始导入管理员角色基础数据");
            for (AdminRole adminRole : adminRoles) {
                if (adminRoleMapper.existsByUuId(adminRole.getUuid())) {
                    continue;
                }
                adminRoleMapper.insert(adminRole);
            }
            log.info("导入管理员角色基础数据完成");
            log.info("读取menu.json配置文件");
            // 读取menu.json配置
            String menuJson = ResourceUtil.readUtf8Str("json/menu.json");
            List<Menu> menus = JSONUtil.toList(menuJson, Menu.class);
            log.info("开始导入菜单基础数据");
            for (Menu menu : menus) {
                if (menuMapper.existsByUuId(menu.getUuid())) {
                    continue;
                }
                menuMapper.insert(menu);
            }
            log.info("导出菜单基础数据完成");
            log.info("读取role_menu.json配置文件");
            String roleMenuJson = ResourceUtil.readUtf8Str("json/role_menu.json");
            List<RoleMenu> roleMenus = JSONUtil.toList(roleMenuJson, RoleMenu.class);
            log.info("开始导入角色菜单基础数据");
            for (RoleMenu roleMenu : roleMenus) {
                if (roleMenuMapper.existsByUuId(roleMenu.getUuid())) {
                    continue;
                }
                roleMenuMapper.insert(roleMenu);
            }
            log.info("导入角色菜单基础数据完成");
            // 设置缓存到redis中已导入基础数据
            redisUtil.setStringValue(DataCacheKey.BASIC_DATA_IS_IMPORT, "true");
        }
    }
}
